<?php
include_once 'library/Entity/BaseEntity.php';
class UserSystem extends BaseEntity{
	const ID = 'id';
	const ACCOUNT = 'account';
	const PASSWORD = 'password';
	const EMAIL = 'email';
	const NAME = 'name';
	const BIRTHDAY= 'birthday';
	const GENDER = 'gender';
	const ADDRESS = 'address';
	const PHONE = 'phone';
	const IMAGE = 'image';
	const ROLE = 'role';
	
	const CREATENAME = 'createname';
	const UPDATENAME = 'updatename';
	const ROLENAME = 'rolename';
	const CREATEDATE = 'createdate';
	
	/**
	 * Primarykey column.
	 * @var array
	 */
	public $pkey = array(UserSystem::ID);
	/**
	 * Auto Increment column.
	 * @var string
	 */
	public $sequence = UserSystem::ID;
	/**
	 * Unique column.
	 * @var array
	 */
	public $unique = array(UserSystem::PHONE);
	
	public function getByUserName($username){
		$sql = 'select '.$this->getTable().'.*,role.name as rolename from '.$this->getTable().',role';
		$sql .= ' where account="'.$username.'" and '.$this->getTable().'.role=role.id';
		return $this->dbHelper->fetchAll($sql);
	}
	public function getAllJoin(){
		$sql = 'SELECT u.*,createbyuser.name as '.UserSystem::CREATENAME.', updatebyuser.name as '.UserSystem::UPDATENAME.',roletype.name as '.UserSystem::ROLENAME.'
			,DATE( u.createtime ) AS '.UserSystem::CREATEDATE.' FROM `usersystem` as u
			left join usersystem as createbyuser on u.createby = createbyuser.id  
			left join usersystem as updatebyuser on u.lastupdateby = updatebyuser.id 
			left join role as roletype on u.role = roletype.id';
		return $this->dbHelper->fetchAll($sql);
	}
}